package inporter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * Function: import all tokens from dictionaries
 */
public class DictionaryImporter 
{
	Hashtable<String, String> htDictionary;
	
	public DictionaryImporter(String pFolderLoc)
	{
		// get file list where the path has   
		File file = new File(pFolderLoc);   
		// get the folder list   
		File[] dicArray = file.listFiles(); 

		htDictionary = new Hashtable<String, String>();
				
		for (int i = 0; i < dicArray.length; i++)
		{
			if (dicArray[i].getName().contains(".txt"))
			{
				getCntDic(dicArray[i]);
			}
		}
				
	}
			
	private void getCntDic(File pCntDic)
	{
		try
		{
			FileReader reader = new FileReader(pCntDic);
			BufferedReader br = new BufferedReader(reader);
			String strCntLine = null;
			
			while((strCntLine = br.readLine()) != null) 
			{
				String s = "\\w+";
		        Pattern  pattern=Pattern.compile(s);  
		        Matcher  ma=pattern.matcher(strCntLine);  
		        String strTmp;
		        while(ma.find())
		        {
		        	strTmp = ma.group();
		       		htDictionary.put(strTmp.trim(), strTmp.trim()); 
		        }  
			}
					
			br.close();
			reader.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public Hashtable<String, String> getDicTokenArray()
	{
		return htDictionary;
	}
}
